package  
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	/**
	 * ...
	 * @author UWGameDevClub
	 */
	public class Item extends Sprite
	{
		protected var _bitmap:Bitmap;
		private var _name:String;
		private var _selected:Boolean;
		
		public function Item( image:Class, name:String ) 
		{
			if ( image != null )
			{
				//Create an instance of the bitmap
				_bitmap = new image();
				
				//Add it to the stage
				addChild(_bitmap);
			}
			else _bitmap = null;
			
			//Set the name
			_name = name;
			
			_selected = false;
			
			addEventListener(MouseEvent.MOUSE_MOVE, mouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, mouseOut);
		}
		
		public function mouseOver(event:MouseEvent):void {
			//if (Main.main.paused) return;
			if (!_selected && pixelTest(event.localX, event.localY)) {
				filters = [ new GlowFilter(0xFFFF00, 1, 7, 7, 3) ];
			}
		}
		
		public function mouseOut(event:MouseEvent):void {
			if (!_selected) filters = [];
		}
		
		public function toggleSelect():void {
			if(!_selected) {
				filters = [ new GlowFilter(0x00ddFF, 1, 7, 7, 3) ];
				_selected = true;
				if (select != null) select();
				else if (interact != null) interact();
			} else {
				filters = [];
				_selected = false;
			}
		}
		
		public function pixelTest( x_:Number, y_:Number ):Boolean
		{
			if ( _bitmap == null ) {
				return (x_ > 0 && x_ < width && y_ > 0 && y_ < height );
			}
			var color:uint = _bitmap.bitmapData.getPixel32(x_, y_) ;
			if ( (color & 0xFF000000) == 0 )
			{
				return false;
			}
			return true;
		}
		public function getName( ):String { return _name; }
		
		public var combine:Function = null;
		public var interact:Function = null;
		public var select:Function = null;
		
	}

}